package edu.zut.cs.software.example.market.web.spring.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.market.dao.ProductDao;
import edu.zut.cs.software.example.market.domain.Product;
import edu.zut.cs.software.example.product.service.ProductManager;
import edu.zut.cs.software.example.student.domain.Student;






/**
 * 
 * @author 耿昊天
 *
 */
@RestController
public class ProductController {
    
    @Autowired
    ProductDao productDao;
    
    @Autowired
	ProductManager productManager;

    @RequestMapping(value="/products")
    public String products(QueryInfo queryInfo) {//传来的数据转换为对象json->obj
		int numbers = productDao.getProductCounts();
		System.out.println("****************"+queryInfo+"*********"+queryInfo.getPageNum()+"********"+queryInfo.getPageSize());
		int pagenum = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
		System.out.println("--"+pagenum);
		List<Product> pro = productDao.getByProducts(pagenum, queryInfo.getPageSize());
		System.out.println("Product:" + pro);
		HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
		res.put("numbers", numbers);
		res.put("Product",pro);//收容传来的对象
		String res_json=JSON.toJSONString(res);//把一个对象转换为json串:把product对象转变成json数据
		System.out.println(res_json);
		return res_json;//含有两块信息回应给前端
	}
    @RequestMapping("/addproducts")
	public String addproducts(@RequestBody Product product) {//传来的数据转换为对象json->obj
    	System.out.println("*****Product******:" + product);
    	//List<Product> addpro = productDao.save(product);
    	Product addpro = productDao.save(product);
    	return null;
	}//
    @RequestMapping("/getByName")
	public String getProductByName(QueryInfo queryInfo) {
		System.out.println("前端携带数据： "+queryInfo+" 请求/getByName");
		
		int numbers = productDao.getProductByNameCounts(queryInfo.getQuery());
//		System.out.println("numbers:"+numbers);
		
		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//		System.out.println("pageStart:"+pageStart);
		
		List<Product> Products= productDao.findByName(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
		System.out.println("查询结果列表："+Products);
		
		HashMap<String,Object> res=new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Products);
		
		String res_string=JSON.toJSONString(res);
		
//		System.out.println("HashMap res: "+res);
//		System.out.println("转换数据格式后 res_string： "+res_string);
		
		return res_string;
	}
    @RequestMapping("/getBySort")
	public String getProductBySort(QueryInfo queryInfo) {
		System.out.println("前端携带数据： "+queryInfo+" 请求/getBySort");
		
		int numbers = productDao.getProductBySortCounts(queryInfo.getQuery());
//		System.out.println("numbers:"+numbers);
		
		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//		System.out.println("pageStart:"+pageStart);
		
		List<Product> Products= productDao.findBySort(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
		System.out.println("查询结果列表："+Products);
		
		HashMap<String,Object> res=new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Products);
		
		String res_string=JSON.toJSONString(res);
		
//		System.out.println("HashMap res: "+res);
//		System.out.println("转换数据格式后 res_string： "+res_string);
		
		return res_string;
	}
    
    @RequestMapping("/deleteByID")
    public String deleteByID(Long id)
    {
    	System.out.println("*****ID******:" + id);
    	int i=productDao.deleteProduct(id); 
    	System.out.println("*****result******:" + i);
    	String str = i >0?"success":"error";
    	System.out.println("*****result******:" + str);
        return str;
    }

    @RequestMapping("/products2")
 	public String products2(QueryInfo queryInfo) {
 		System.out.println("前端携带数据： "+queryInfo+" 请求/getById");
 		
 		int numbers = productDao.getProductByStudentIdCounts(queryInfo.getId());
 		System.out.println("numbers:"+numbers);
 		
 		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
// 		System.out.println("pageStart:"+pageStart);
 		
 		List<Product> Products= productDao.findByStudentId(queryInfo.getId(), pageStart, queryInfo.getPageSize());
 		System.out.println("查询结果列表："+Products);
 		
 		HashMap<String,Object> res=new HashMap<>();
 		res.put("numbers", numbers);
 		res.put("data", Products);
 		
 		String res_string=JSON.toJSONString(res);
 		
// 		System.out.println("HashMap res: "+res);
// 		System.out.println("转换数据格式后 res_string： "+res_string);
 		
 		return res_string;
 	}
    
    /**
	 * 通过指定id查询商品信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getupdateproduct")
	public String getUpdateProduct(int id) {
		return JSON.toJSONString(productManager.getUpdateProService(id));
	}

	/**
	 * 编辑修改商品资料
	 * @param VueProduct
	 * @return
	 */
	@RequestMapping("/editproduct")
	public String editProductInfo(@RequestBody Product product) {
		String flag="error";
		System.out.println(product);
		int resCode=productManager.editProInfoService(product);
		System.out.println("??????"+resCode);
		if(resCode==1) flag="success";
		
		HashMap<String,Object> res=new HashMap<String, Object>();
		res.put("flag", flag);
		
		int id=Integer.parseInt(String.valueOf(product.getId()));
		
		Product newProInfo=productManager.getUpdateProService(id);
		
		res.put("product", newProInfo);
		
		String r =  JSON.toJSONString(res);

		return r;
	}
	}
    /*@CrossOrigin
    @RequestMapping("/allProduct")
    public String getProductList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = productDao.getProductCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List products = productDao.getAllProduct("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",products);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/productState")
    public String updateProductState(@RequestParam("id") Integer  id,
                                  @RequestParam("state") Boolean state){
        int i = productDao.updateState(id, state);
        System.out.println("商品编号:"+id);
        System.out.println("商品状态:"+state);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/addProduct")
    public String addProduct(@RequestBody Product product){
        System.out.println(product);
        product.setSort("所有商品");
        product.setState(false);
        int i = productDao.addProduct(product);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdate")
    public String getUpdateProduct(int id){
        System.out.println("编号:"+id);
        Product updateProduct = (Product) productDao.getUpdateProduct(id);
        String products_json = JSON.toJSONString(updateProduct);
        return products_json;
    }

    @RequestMapping("/editProduct")
    public String editProduct(@RequestBody Product product){
        System.out.println(product);
        int i = productDao.editProduct(product);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteProduct")
    public String deleteProduct(int id){
        System.out.println(id);
        int i = productDao.deleteProduct(id);
        String str = i >0?"success":"error";
        return str;
    }
}*/
